import { STORE_KEY } from "@/common/config";
import { handleShopServiceType } from "@/common/service";
import { IResponseSuccess, IShopServiceType, IShopServiceTypeData } from "@/typing";
import Taro from "@tarojs/taro";
import { useState } from "react";

const useSeviceTypeAction = () => {
    const [serviceTypeList, setServiceTypeList] = useState<Array<IShopServiceTypeData>>([])
    const handleInitServiceTypeList = async () => {
        const list = await handleShopServiceType() as IResponseSuccess<IShopServiceType>
        //    const {data:{data}} = list.data.data;
        if (list.data.data && list.data.data.length > 0) {
            setServiceTypeList(list.data.data)
        }

    }
    const handleInitServiceTypeDescT = (serviceIdList: Array<number>) => {

        Taro.setStorageSync(STORE_KEY.ADD_SERVICE_TYPE, JSON.stringify(serviceIdList));
       
        const selectedTypeNames = serviceIdList.map(id => {
            const selectedType = serviceTypeList.find(item => item.id === id);
            return selectedType ? selectedType.name : null;
        }).filter(name => name !== null);
        Taro.setStorageSync(STORE_KEY.ADD_SERVICE_TYPE_DESC, JSON.stringify(selectedTypeNames));
        const storeTypeDesc = selectedTypeNames.join('、')
        return storeTypeDesc
    }
    const handleInitSubmitServiceTypeInfo = (serviceIdList: Array<number>) => {
        const computedServiceObj = {}
        serviceIdList.forEach((typeId, index) => {

            const keyIdVal = `service_type_id[${[index]}]`
            computedServiceObj[keyIdVal] = typeId


        })
        return computedServiceObj
       
    }
    return {
        serviceTypeList,
        setServiceTypeList,
        handleInitServiceTypeList,
        handleInitServiceTypeDescT,
        handleInitSubmitServiceTypeInfo
    }
}
export default useSeviceTypeAction;
